package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
    @Autowired
    private TbBusinessTrackRecordMapper mapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private TbBusinessMapper businessMapper;

    @Override
    public List<TbBusinessTrackRecord> record(Long businessId) {
        List<TbBusinessTrackRecord> record = mapper.record(businessId);
        List<TbBusinessTrackRecord> collect = record.stream().map(new Function<TbBusinessTrackRecord, TbBusinessTrackRecord>() {
            @Override
            public TbBusinessTrackRecord apply(TbBusinessTrackRecord tbBusinessTrackRecord) {
                String keyItems = tbBusinessTrackRecord.getKeyItems();
                //查询对应的重点信息 查询字段表
                List<String> keys = dictDataMapper.select(keyItems);
                tbBusinessTrackRecord.setKeys(keys);
                return tbBusinessTrackRecord;
            }
        }).collect(Collectors.toList());
        return collect;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(BusinessTrackVo businessTrackVo) {
        //创建商品操作表
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        //复制vo中的数据
        BeanUtils.copyProperties(businessTrackVo,tbBusinessTrackRecord);
        tbBusinessTrackRecord.setCreateTime(new Date());
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());


        //添加到商品操作历史表中
        mapper.insert(tbBusinessTrackRecord);
        //修改商品信息
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo,tbBusiness);
        businessMapper.updateTbBusiness(tbBusiness);
    }
}
